package com.gnerv.sylvanas.business.manage.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.business.manage.logic.ApiGroupLogicService;
import com.gnerv.sylvanas.business.manage.model.dto.ApiGroupCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiGroupDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiGroupTreeDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiGroupTreeDetailsDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiGroupUpdateDTO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/manage/api/group")
public class ApiGroupController {

    private final ApiGroupLogicService apiGroupLogicService;

    @PostMapping(value = "")
    public RestResult<Void> create(@RequestBody ApiGroupCreateDTO apiGroupCreateDTO) {
        return apiGroupLogicService.create(apiGroupCreateDTO);
    }

    @DeleteMapping(value = "")
    public RestResult<Void> deleteByUkId(@RequestParam(value = "ukId") String ukId) {
        return apiGroupLogicService.deleteByUkId(ukId);
    }

    @PutMapping(value = "")
    public RestResult<Void> updateByUkId(@RequestBody ApiGroupUpdateDTO apiGroupUpdateDTO) {
        return apiGroupLogicService.updateByUkId(apiGroupUpdateDTO);
    }

    @GetMapping(value = "")
    public RestResult<ApiGroupDTO> selectByUkId(@RequestParam(value = "ukId") String ukId) {
        return apiGroupLogicService.selectByUkId(ukId);
    }

    @GetMapping(value = "/list")
    public RestResult<List<ApiGroupDTO>> selectListByName(@RequestParam(required = false, value = "name") String name,
                                                          @RequestParam(required = false, value = "page") int page,
                                                          @RequestParam(required = false, value = "size") int size) {
        return apiGroupLogicService.selectListByName(name, page, size);
    }

    @GetMapping(value = "/list/tree")
    public RestResult<List<ApiGroupTreeDTO>> selectListTreeByParentUkId(@RequestParam(value = "parentUkId") String parentUkId) {
        return apiGroupLogicService.selectListTreeByParentUkId(parentUkId);
    }

    @GetMapping(value = "/details/list/tree")
    public RestResult<List<ApiGroupTreeDetailsDTO>> selectListTreeDetailsByParentUkId(@RequestParam(value = "parentUkId") String parentUkId) {
        return apiGroupLogicService.selectListTreeDetailsByParentUkId(parentUkId);
    }

}
